define(function(require ,exports ,module){
	
	var util = require("/core/util");
    // exports
    var controllers = {
        operation : ['$scope' ,'ServicesProvider','$rootScope' ,
            function($scope ,ServicesProvider ,$rootScope){
                findList($scope ,ServicesProvider ,$rootScope);
            }
        ]
    };

    function findList($scope ,ServicesProvider ,$rootScope){
        var service = ServicesProvider.category;
        var cols = [
			{
				width : '40px',
				displayName : '序号',
				cellTemplate : '<div class="ngCellText" ng-class="col.colIndex()"><span ng-cell-text>{{((pagingOptions.currentPage - 1) * pagingOptions.pageSize) + row.rowIndex + 1}}</span></div>'
			},
			{field:'name', displayName:'name'},
			{
				field:'_id',
				displayName : '操作',
				cellTemplate : '/core/tpl/EditCellTemplate.html'
			}
		];

        $scope.totalServerItems = 0;
        $scope.setPagingData = function(){
            service.get(function (result) {
				$rootScope.message = null;
                if(result.data.length == 0){
                    $rootScope.message = {type : 'info' , text : '暂时没有数据...'};
                }
                //console.log(result.data);
                $scope.data = result.data;
                if (!$scope.$$phase) {
                    $scope.$apply();
                }
//                if(result.code == 200){
//
//                }else{
//                    $rootScope.message = {type : 'error' , text : 'Code : ' + result.code + ' Message : ' + result.data};
//                }
            });
        };
        $scope.setPagingData();
        $scope._category = {};
        $scope.selectedItems = [];
        $scope.$watch('selectedItems[0]',function(){
			//console.log($scope.selectedItems[0]);
			$scope._category = angular.copy($scope.selectedItems[0],$scope._category);
        });

        $scope.filterOptions = { filterText: '', useExternalFilter: true };
        $scope.gridOptions = {
            columnDefs : cols,
            data : 'data',
            multiSelect : false,
            showFooter: true,
            selectedItems : $scope.selectedItems
        };


        //-------------
        $scope.cellDelete = function(_id){
            event.stopPropagation();
            service.delete({_id : _id},function(){
                $scope.setPagingData();
            });
        }
        $scope.save = function(){
            service.save({
                name : $scope._category.name,
                _id : $scope._category._id
            } ,function(result){
                $scope.setPagingData();
                $scope._category = {};
            });
        };
		$scope.insert = function(){
            service.save({
                name : $scope._category.name
            } ,function(result){
                $scope.setPagingData();
                $scope._category = {};
            });
        };
    }

    util.namespace("blog4node.admin.category.controller");
    return blog4node.admin.category.controller = controllers;
})